package com.buaa.airticketbookingsystem.Controller;

import com.buaa.airticketbookingsystem.Entity.FavouriteRecord;
import com.buaa.airticketbookingsystem.Entity.Flight;
import com.buaa.airticketbookingsystem.Service.FavouriteService;
import com.buaa.airticketbookingsystem.Service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class SearchFlightController {
    @Autowired
    private FlightService flightService;

    @Autowired
    private FavouriteService favouriteService;

    @PostMapping("/search")
    public Map<String, Object> getFlights(@RequestBody Map<String, String> remap){
        String date = remap.get("date");
        String from = remap.get("from");
        String email = remap.get("email");
        String to = remap.get("to");
        String time = remap.get("time");
        System.out.println(from);
        System.out.println(to);
        System.out.println(date);
        Map<String, Object> res_map = new HashMap<>();
        List<Flight> flight_list = (ArrayList<Flight>) flightService.getAllFlights(date, from, to, time);
        System.out.println(flight_list.size());
        List<Integer> flag_list = new ArrayList<>();
        if(flight_list == null || flight_list.size() == 0){
            res_map.put("res", false);
            res_map.put("message", "当前没有满足条件的航班");
        }
        else{
            res_map.put("res", true);
            res_map.put("message", "查询航班信息成功");
            res_map.put("flight_list", flight_list);
            for(Flight f : flight_list){
                FavouriteRecord res =  favouriteService.searchFavouriteRecordByEmailAndF_id(email, f.getF_id());
                if(res == null){
                    flag_list.add(0);
                }
                else{
                    flag_list.add(1);
                }
            }
            res_map.put("flag_list", flag_list);
        }
        return res_map;
    }

    @PostMapping("/search_fid")
    public Map<String, Object> searchFlightByF_id(@RequestBody Map<String, String> remap){
        String f_id = remap.get("f_id");
        String email = remap.get("email");
        FavouriteRecord res =  favouriteService.searchFavouriteRecordByEmailAndF_id(email, f_id);
        Map<String, Object> resMap = new HashMap<>();
        if(res == null){
            resMap.put("flag", 0);
        }
        else{
            resMap.put("flag", 1);
        }
        try{
            Flight f = flightService.selectFlightByID(f_id);
            if(f == null){
                resMap.put("res", false);
                resMap.put("message", "查询失败，当前航班号所对应航班为空");
            } else{
                resMap.put("res", true);
                resMap.put("message", "查询成功");
                resMap.put("flight", f);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        return resMap;
    }
}
